package com.hd.manager.util;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.RandomStringUtils;
import org.springframework.boot.system.ApplicationHome;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;

public class QrCodeUtil {

    // 二维码的宽度
    static final int WIDTH = 450;
    // 二维码的高度
    static final int HEIGHT = 450;
    // 二维码的格式
    static final String FORMAT = "png";
    // 二维码的内容
    static final String TEXT = "Hello！二维码！！！";


    private static final int red = 0xFF0000; // 码颜色
//    private static final int blue = 0x0000FF;  // 码颜色
//    private static final int blue = 0x459DF5;  // 码颜色
    private static final int blue = 0x0099FF;
//    #459df5
//    08c1ff
    private static final int yellow = 0xFFFF00;  // 码颜色
//    private static final int purple = 0xCC00CC; // 码颜色
    private static final int purple = 0xFF15F1; // 码颜色


//    @Test
//    public void test() {
//        generate("xxxx", "xxx", 1);
//
//    }


    /**
     * 生成二维码
     */
    public static String generate(String userid, String str, int color) {
       /*
           定义二维码的参数
        */
        String filename= RandomStringUtils.randomAlphanumeric(20);

        HashMap hashMap = new HashMap();
        // 设置二维码字符编码
        hashMap.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        // 设置二维码纠错等级
        hashMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
        // 设置二维码边距
        hashMap.put(EncodeHintType.MARGIN, 2);
        String base64String = null;
        try {
            // 开始生成二维码
            BitMatrix bitMatrix = new MultiFormatWriter().encode(filename+TEXT, BarcodeFormat.QR_CODE, WIDTH, HEIGHT, hashMap);
            // 导出到指定目录

            int HUA_SE = 0x000000; // 码颜色

            switch (color) {
                case 1:
                    HUA_SE = red;
                    break;

                case 2:
                    HUA_SE = yellow;
                    break;

                case 3:
                    HUA_SE = purple;
                    break;

                case 4:
                    HUA_SE = blue;
                    break;
            }

            int DI_SE = 0xFFFFFF; // 底色


            int b_width = bitMatrix.getWidth();
            int b_height = bitMatrix.getHeight();
            // 创建图像缓冲器
            BufferedImage image = new BufferedImage(b_width, b_height, BufferedImage.TYPE_3BYTE_BGR);
            for (int x = 0; x < b_width; x++) {
                for (int y = 0; y < b_height; y++) {
                    image.setRGB(x, y, bitMatrix.get(x, y) ? HUA_SE : DI_SE);
                }
            }
//            ClassPathResource classPathResource = new ClassPathResource("static/");
//            String path="/home/www/jar/static/";
            String path = "/static/";

//            String staticpath=this.mvcProperties.getProperty("static");
            File outputFile = new File(path);
            if (!outputFile.exists()) {
                // 建立文件夹
                outputFile.mkdir();
            }

            // 生成二维码
            ImageIO.write(image, FORMAT, new File(path + userid + "." + FORMAT));
            // 二维码的名称
            try {
                //图片转base64
                ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                ImageIO.write(image, FORMAT, outputStream);
                byte[] bytes = outputStream.toByteArray();
                base64String = Base64.encodeBase64String(bytes);
                System.out.println(base64String);

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
//            MatrixToImageWriter.writeToPath(bitMatrix, FORMAT, new File(getFilePath(userid+".png")).toPath());
        } catch (WriterException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return base64String;
    }

    private String getFilePath(String UPLOAD_TEMP_FILE_NAME) {
        ApplicationHome h = new ApplicationHome(getClass());
        File jarF = h.getSource();
        System.out.println(jarF.getParentFile().toString());
        String path = jarF.getParentFile().toString();
        int i = path.lastIndexOf("/");
        if (i > 0) {
            path = path.substring(0, i + 1);
        }
        path += UPLOAD_TEMP_FILE_NAME;
        System.out.println(path);
        return path;
    }


}
